﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace YATC
{
    class StartScene : GameScene
    {
        private Model title;
        private Matrix gameWorldRotation = Matrix.Identity;
        private float rot;
        private float[] rotation;
        private bool turn;
        private Model[] menu;
        private int selected;
        private KeyboardState oldS;
        private AudioLibrary audioLibrary;

        public StartScene(Game game, Model title,Model[] menu)
            : base(game)
        {
            this.title = title;
            this.menu = menu;
            selected = 0;
            oldS = Keyboard.GetState();
            audioLibrary = (AudioLibrary)Game.Services.GetService(typeof (AudioLibrary));
            MediaPlayer.Stop();
            rotation = new float[5];
        }
        public int Selected
        {
            get{ return selected;}
        }
        public override void Update(GameTime gameTime)
        {
            rot += turn ? -0.6f : 0.6f;
            if (rot > 20)
                turn = true;
            if (rot < -20)
                turn = false;
            gameWorldRotation = Matrix.CreateRotationY(MathHelper.ToRadians(rot));

            HundelInput();

            base.Update(gameTime);
        }
        private void HundelInput()
        {
            
            
            var curS = Keyboard.GetState();
            if (curS.IsKeyDown(Keys.Up) && oldS.IsKeyUp(Keys.Up))
            {
                selected--;
                audioLibrary.MenuSelect.Play();
            }
            if (curS.IsKeyDown(Keys.Down) && oldS.IsKeyUp(Keys.Down))
            {
                selected++;
                audioLibrary.MenuSelect.Play();
            }

            oldS = curS;
            if (selected == -1)
                selected = 4;
            else if (selected == 5)
                selected = 0;
        }
        public override void Draw(GameTime gameTime)
        {
            DrawTitle();

            DrawNew(menu[0], selected == 0 ? true : false);
            DrawHi(menu[1], selected == 1 ? true : false);
            DrawOp(menu[2], selected == 2 ? true : false);
            DrawCre(menu[3], selected == 3 ? true : false);
            DrawEx(menu[4], selected == 4 ? true : false);

            base.Draw(gameTime);
        }
        private void DrawNew(Model m,bool select)
        {
            if (select)
                rotation[0] += 0.05f;
           else
                rotation[0] = 0.0f;


            Matrix _gameWorldRotation = Matrix.CreateRotationX(rotation[0]);

             var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = _gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,0,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }
        private void DrawHi(Model m,bool select)
        {
            if (select)
                rotation[1] += 0.05f;
           else
                rotation[1] = 0.0f;


            Matrix _gameWorldRotation = Matrix.CreateRotationX(rotation[1]);

             var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = _gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,-2,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }


        private void DrawOp(Model m,bool select)
        {
            if (select)
                rotation[2] += 0.05f;
           else
                rotation[2] = 0.0f;


            Matrix _gameWorldRotation = Matrix.CreateRotationX(rotation[2]);

             var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = _gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,-4,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }

        private void DrawCre(Model m,bool select)
        {
            if (select) rotation[3] += 0.05f;
           else rotation[3] = 0.0f;


            Matrix _gameWorldRotation = Matrix.CreateRotationX(rotation[3]);

            var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = _gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,-6,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }

        private void DrawEx(Model m,bool select)
        {
            if (select) rotation[4] += 0.05f;
            else rotation[4] = 0.0f;


            Matrix _gameWorldRotation = Matrix.CreateRotationX(rotation[4]);

            var transforms = new Matrix[m.Bones.Count];
            m.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in m.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = _gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,-8,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }


        private void DrawTitle()
        {
            var transforms = new Matrix[title.Bones.Count];
            title.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in title.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
  

                    effect.View = Matrix.CreateLookAt(new Vector3(0,0,30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = gameWorldRotation*
                                   transforms[mesh.ParentBone.Index]*
                                   Matrix.CreateTranslation(new Vector3(0,6,0))*Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }

        public override void Show()
        {
            audioLibrary.StartScene.Play();
            Game.IsMouseVisible = false;
            base.Show();
        }
    }
}
